package hut.ai.team.mgr;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;

public class SoundMrg {
	private static final String NAME_SOUND = "name_sound";
	private static final String SOUND = "sound";

	public static final int SOUND_BG = 1;
	public static final int SOUND_CLICK = 2;
	public static final int COMBOX2 = 3;
	public static final int COMBOX3 = 4;
	public static final int COMBOX4 = 5;
	public static final int COMBOX5 = 6;
	public static final int BG_FAILURE = 7;
	public static final int MISS = 8;
	public static final int WIN = 9;

	private static SoundMrg instance;
	private Array<Sound> arrSound;
	private Music musicbg;
	private boolean isSoundon = true;
	private String[] pathSound = new String[] { "sounds/click.mp3", "sounds/combox2.mp3", "sounds/combox3.mp3", "sounds/combox4.mp3",
			"sounds/combox5.mp3", "sounds/fail.mp3", "sounds/win.mp3", "sounds/miss.mp3" };

	public static void initSound() {
		instance = new SoundMrg();
		instance.isSoundon = LoadSoundGame();
	}

	private SoundMrg() {
		FileHandle filesound = Gdx.files.internal("sounds/bg.mp3");
		musicbg = Gdx.audio.newMusic(filesound);
		arrSound = new Array<Sound>();
		for (int i = 0; i < pathSound.length; i++) {
			Sound sound = Gdx.audio.newSound(Gdx.files.internal(pathSound[i]));
			arrSound.add(sound);
		}

	}

	public static SoundMrg getInstance() {
		return instance;
	}

	public void play(int sound) {
		if (isSoundon) {
			switch (sound) {
			case SOUND_BG:
				playBG();
				break;
			case SOUND_CLICK:
				arrSound.get(0).play();
				break;
			case COMBOX2:
				arrSound.get(1).play();
				break;
			case COMBOX3:
				arrSound.get(2).play();
				break;
			case COMBOX4:
				arrSound.get(3).play();
				break;
			case COMBOX5:
				arrSound.get(4).play();
				break;
			case BG_FAILURE:
				arrSound.get(5).play();
				break;
			case WIN:
				arrSound.get(6).play();
				break;
			case MISS:
				arrSound.get(7).play();
				break;
			}
		}
	}

	public boolean isSound() {
		return isSoundon;
	}

	public void enableSound(boolean isSound) {
		this.isSoundon = isSound;
		SaveSoundGame(isSoundon);
		if (!isSound) {
			stop();
		}
	}

	public void stop() {
		musicbg.stop();
		for (int i = 0; i < arrSound.size; i++) {
			arrSound.get(i).stop();
		}
	}

	private void playBG() {
		
		if (!musicbg.isPlaying()) {
			musicbg.setLooping(true);
			musicbg.play();
		}
	}

	public void distroy() {
		musicbg.dispose();
		for (int i = 0; i < arrSound.size; i++) {
			arrSound.get(i).dispose();
		}
		arrSound.clear();
		arrSound = null;
	}

	public static void SaveSoundGame(boolean isSound) {
		Preferences pref = Gdx.app.getPreferences(NAME_SOUND);
		pref.putBoolean(SOUND, isSound);
		pref.flush();
	}

	public static boolean LoadSoundGame() {
		Preferences pref = Gdx.app.getPreferences(NAME_SOUND);
		return pref.getBoolean(SOUND, true);
	}

}
